package cn.tqfeiyang.smartadmin.business.organization.controller;

import cn.tqfeiyang.smartadmin.business.organization.constant.AddressConstants;
import cn.tqfeiyang.smartadmin.business.organization.service.AddressCurdService;
import cn.tqfeiyang.smartadmin.business.organization.request.AddressCreateDTO;
import cn.tqfeiyang.smartadmin.business.organization.request.AddressUpdateDTO;
import cn.tqfeiyang.smartadmin.business.organization.request.AddressQueryDTO;
import cn.tqfeiyang.smartadmin.business.organization.response.AddressQueryVO;
import cn.tqfeiyang.smartadmin.business.organization.response.AddressDetailVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;

import java.util.List;

/**
 * 地址CURD控制器
 *
 * @author tqfeiyang
 * @since 2024-10-18
 */
@RestController
@Validated
@RequestMapping(AddressConstants.REQUEST_URL)
@Tag(name = AddressConstants.API_TAG)
public class AddressCurdController {

    /**
     * 地址CURD服务
     */
    @Resource
    private AddressCurdService addressCurdService;

    /**
     * 分页查询地址
     */
    @OperationLog
    @GetMapping("/queryAddresses")
    @Operation(summary = "地址 - 分页查询地址")
    @SaCheckPermission(AddressConstants.PERMISSION + "queryAddresses")
    public ApiResult<PageData<AddressQueryVO>> queryAddresses(@Validated AddressQueryDTO dto) {
        return ApiResult.success(addressCurdService.queryAddresses(dto));
    }

    /**
     * 查询地址详情
     */
    @OperationLog
    @GetMapping("/readAddress/{addressId}")
    @Operation(summary = "地址 - 查询地址详情")
    @SaCheckPermission(AddressConstants.PERMISSION + "readAddress")
    public ApiResult<AddressDetailVO> readAddress(@PathVariable @EntityId Long addressId) {
        return ApiResult.success(addressCurdService.readAddress(addressId));
    }

    /**
     * 添加地址
     */
    @OperationLog
    @PostMapping("/createAddress")
    @Operation(summary = "地址 - 添加地址")
    @SaCheckPermission(AddressConstants.PERMISSION + "createAddress")
    public ApiResult<AddressDetailVO> createAddress(@RequestBody @Validated AddressCreateDTO dto) {
        Long addressId = addressCurdService.createAddress(dto);
        return ApiResult.success(addressCurdService.readAddress(addressId));
    }

    /**
     * 修改地址
     */
    @OperationLog
    @PostMapping("/updateAddress/{addressId}")
    @Operation(summary = "地址 - 修改地址")
    @SaCheckPermission(AddressConstants.PERMISSION + "updateAddress")
    public ApiResult<AddressDetailVO> updateAddress(@PathVariable @EntityId Long addressId, @RequestBody @Validated AddressUpdateDTO dto) {
        addressCurdService.updateAddress(addressId, dto);
        return ApiResult.success(addressCurdService.readAddress(addressId));
    }

    /**
     * 删除地址
     */
    @OperationLog
    @PostMapping("/deleteAddress/{addressId}")
    @Operation(summary = "地址 - 删除地址")
    @SaCheckPermission(AddressConstants.PERMISSION + "deleteAddress")
    public ApiResult deleteAddress(@PathVariable @EntityId Long addressId) {
        addressCurdService.deleteAddress(addressId);
        return ApiResult.success();
    }

    /**
     * 批量删除地址
     */
    @OperationLog
    @PostMapping("/batchDeleteAddresses")
    @Operation(summary = "地址 - 批量删除地址")
    @SaCheckPermission(AddressConstants.PERMISSION + "batchDeleteAddresses")
    public ApiResult batchDeleteAddresses(@RequestBody List<Long> addressIds) {
        addressCurdService.batchDeleteAddresses(addressIds);
        return ApiResult.success();
    }
}
